﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data;
using Microsoft.SharePoint;
using CodeArt.SharePoint.CamlQuery;

namespace TestProject1
{
    /// <summary>
    /// UnitTest1 的摘要说明
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        //[TestMethod]
        //public void TestGetItemFromView()
        //{
        //    var url = "http://localhost:81/sites/s2";

        //    SPSite site = new SPSite(url);

        //    SPList list = site.RootWeb.Lists["任务"];

        //    QueryField field1 = new QueryField("Title");

        //    SPListItemCollection items =
        //        ListQuery.Select(field1)
        //        .From(list.DefaultView)
        //        //.Where(expr)
        //        .OrderBy(field1)
        //        .GetItems();

        //    Assert.IsTrue(items.Count == 1);
        //}

        #region 其他测试属性
        //
        // 您可以在编写测试时使用下列其他属性:
        //
        // 在运行类中的第一个测试之前使用 ClassInitialize 运行代码
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // 在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // 在运行每个测试之前使用 TestInitialize 运行代码 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // 在运行每个测试之后使用 TestCleanup 运行代码
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion


        //test data:
        //column:标题，正文
        //Test3   2008-2-4 21:58 Test3Test3Test3 
        //Test2   2008-2-4 21:58 Test2Test2Test2Test2 
        //Test1   2008-2-4 21:58 Test1Test1Test1Test1 

        [TestMethod]
        public void TestBasicQuery()
        {

            SPSite site = new SPSite("http://jyserver:81");

            SPList list = site.RootWeb.Lists["Notice"];

            QueryField field1 = new QueryField("Field1");

            CamlExpression expr = field1 == "test1" || field1.Contains("aa");

            SPListItemCollection items =
                ListQuery.Select(field1)
                .From(list)
                .Where(expr)
                .OrderBy( field1 )
                .GetItems();

            Assert.IsTrue(items.Count == 1);

        }

        [TestMethod]
        public void TestWhereAndOrderBy()
        {
            SPSite site = new SPSite("http://moss2007:82/sites/precal/");

            SPList list = site.RootWeb.Lists["PreCalItem"];

            TypedQueryField<int> field1 = new  TypedQueryField<int> ("Sequence");            

            var items =
                ListQuery
                .From(list).Where(field1>10)        
                .OrderBy(field1, false)
                .GetDataTable();

            Assert.IsTrue(items.Rows.Count>0);
        }

        [TestMethod]
        public void TestQueryByDisplayName()
        {
            SPSite site = new SPSite("http://jyserver:81");

            SPList list = site.RootWeb.Lists["Notice"];

            QueryField field1 = new QueryField("标题",false);

            CamlExpression expr = field1.Contains("Test1");

            SPListItemCollection items =
                ListQuery.Select( field1)
                .From(list)
                .Where(expr)
                .OrderBy(field1)
                .GetItems();

            Assert.IsTrue(items.Count == 1);
        }

        [TestMethod]
        public void TestQueryByDisplayName2()
        {
            SPSite site = new SPSite("http://jyserver:81");

            SPList list = site.RootWeb.Lists["Notice"];

            TypedQueryField<DateTime> field1 = new TypedQueryField<DateTime>("修改时间", false);

            CamlExpression expr = field1 >= DateTime.Now.AddDays(-1);

            SPListItemCollection items =
                ListQuery.Select(field1)
                .From(list)
                .Where(expr)
                .OrderBy(field1)
                .GetItems();

            Assert.IsTrue(items.Count == 3);
        }

        [TestMethod]
        public void TestLimit()
        {
            SPSite site = new SPSite("http://jyserver:81");

            SPList list = site.RootWeb.Lists["Notice"];

            QueryField field1 = new QueryField("标题", false);

            SPListItemCollection items =
                ListQuery.Select(1)
                .From(list)
                .Where( field1.Contains("Test") )
                .OrderBy(field1)
                .GetItems();

            Assert.IsTrue(items.Count == 1);
        }

        [TestMethod]
        public void TestOrderBy()
        {
            SPSite site = new SPSite("http://jyserver:81");

            SPList list = site.RootWeb.Lists["Notice"];

            QueryField field1 = new QueryField("标题", false);

            SPListItemCollection items =
                ListQuery
                .From(list)
                .Where(field1.Contains("Test"))
                .OrderBy(field1)
                .GetItems();

            Assert.IsTrue(items[0].Title == "Test1");

            items =
                ListQuery
                .From(list)
                .Where(field1.Contains("Test"))
                .OrderBy(field1,false)
                .GetItems();

            Assert.IsTrue(items[0].Title == "Test3");
        }

        [TestMethod]
        public void TestGetDataTable()
        {
            SPSite site = new SPSite("http://jyserver:81");
            SPWeb web = site.RootWeb;

            SPList list = web.Lists["VacationArchive"];

            int year = DateTime.Now.Year;

            DateTime yearBegin = new DateTime(year, 1, 1);
            DateTime yearEnd = new DateTime(year + 1, 1, 1);

            TypedQueryField<DateTime> createDtField = new TypedQueryField<DateTime>("BeginTime");

            QueryField userField = new QueryField("AuthorRef");

            QueryField dayCountField = new QueryField("TotalDayCount");

            //已经休假数据
            DataTable vacationData =
                ListQuery.Select(userField, dayCountField)
                .From(list)
                .Where(createDtField > yearBegin && createDtField < yearEnd)
                .GetDataTable();
        }

        [TestMethod]
        public void TestUserQuery()
        {
            SPSite site = new SPSite("http://moss2007:82");
            SPWeb web = site.RootWeb;

            SPList list = web.Lists["Tasks"];

            int year = DateTime.Now.Year;

            DateTime yearBegin = new DateTime(year, 1, 1);
            DateTime yearEnd = new DateTime(year + 1, 1, 1);

            TypedQueryField<DateTime> createDtField = new TypedQueryField<DateTime>("BeginTime");

            TypedQueryField<SPUser> userField = new TypedQueryField<SPUser>("Author");

            TypedQueryField<SPUser> AssignedTo = new TypedQueryField<SPUser>("AssignedTo");

            QueryField dayCountField = new QueryField("TotalDayCount");

            SPUser user = web.SiteUsers["MOSS2007\\user1"];

            //已经休假数据
            DataTable vacationData =
                ListQuery.Select(AssignedTo)
                .From(list)
                .Where(AssignedTo == user)
                .GetDataTable();

            Assert.IsTrue(vacationData.Rows.Count > 0);

        }

        [TestMethod]
        public void TestUserQueryByCAML()
        {
        //    SPSite site = new SPSite("http://jyserver:81");
        //    SPWeb web = site.RootWeb;

        //    SPList list = web.Lists["VacationRequest"];

        //    SPQuery q = new SPQuery();
        //    q.Query = "<Where>";

        //    Assert.IsTrue(vacationData.Rows.Count > 0);

        }

        [TestMethod]
        public void TestQueryModeStat()
        {
            QueryField f = new QueryField("f1",true);

            ICamlExpression q = (f.Equal(SPModerationStatusType.Approved));

            var caml = CamlBuilder.Where(q);
        }

    }
}
